#pragma once

#include "common.h"

const int ONLINE_MAX_PACKET_SIZE = 64 * 1024;

enum SocketSelectFlag
{
	SOCKET_SELECT_WRITE = 0,
	SOCKET_SELECT_READ
};

enum SocketType
{
	SOCKET_TYPE_TCP = 0,
	SOCKET_TYPE_UDP
};

void Startup();
void Cleanup();

class Socket
{
public:

	Socket(SocketType socketType)
		: m_socketType(socketType)
		, m_bIsBlocking(true)
		, m_socket(-1)
		, m_bindPort(0)
	{
	}

	virtual ~Socket()	{}
	
	int GetSocket()				{ return m_socket; }
	bool GetBlocking()			{ return m_bIsBlocking; }
	uint16 GetBindPort()	    { return m_bindPort; }
	
	virtual int GetLastError() = 0;

	virtual bool CreateSocket(int socketId = -1) = 0;
	virtual bool CloseSocket() = 0;
	virtual bool SetNonBlocking() = 0;
	virtual bool Bind(uint16 port = 0, char* ip = NULL) = 0;
	virtual bool Listen(int backLop = -1) = 0;
	virtual bool Accept(int& socket, char* &ip, uint16& port) = 0;
	virtual bool Connect(char* ip, uint16 port) = 0;
	virtual bool Send(const char* buffer, int& size) = 0;
	virtual bool Recv(char* &buffer, int& size) = 0;
	virtual bool SendTo(const char* buffer, int& size, char* ip, uint16 port) = 0;
	virtual bool RecvFrom(char* &buffer, int& size, char* &ip, uint16& port) = 0;

	virtual bool Select(SocketSelectFlag selectFlag) = 0;

protected:

	SocketType	m_socketType;	
	bool		m_bIsBlocking;
	int			m_socket;
	uint16		m_bindPort;
	fd_set		m_fdSet;
};